/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.lom2owl.owl;

import org.ieee.ltsc.lom.LOM;
import org.ieee.ltsc.lom.LOMUtil;

import java.io.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;

/**
 * Translates a LOM identifier item to its representation in OWL.  
 *
 */
public class I2LITranslator extends ElementTranslatorImpl {

    private String category =
            new String();
    private String attributeName =
            new String();
    private String element =
            new String();

    /** Creates a new instance of I2LITranslator */
    public I2LITranslator(String elementCategory, String elementElement,
            String elementAttribute) {

        this.category =
                elementCategory;
        this.attributeName =
                elementAttribute;
        this.element =
                elementElement;
    }

    /**
     * 
     * This method translates a LOM educational element to its representation 
     * in OWL. 
     * @param lom The metadata record
     * @param ont The ontology
     * @param inst The instance of learning Object
     * @param nsName The namespace
     * @param errorWriter The error file
     */
    public void apply(LOM lom, OntModel ont, Individual inst, String nsName,
            PrintWriter errorWriter) {

        String instance =
                null;

        DatatypeProperty catalogProperty =
                ont.getDatatypeProperty(nsName +
                "#inCatalog");
        DatatypeProperty entryProperty =
                ont.getDatatypeProperty(nsName +
                "#isEntry");


        // 1) Get the information from lom xml file

        String catalogLOM =
                "";
        String entryLOM =
                "";

        if (this.category.equals("General")) {
            //1.1 identifier
            if (this.element.equals("IdentifierLO")) {

                OntClass lomIdClass =
                        ont.getOntClass(nsName + "#lomIdentifier");
                ObjectProperty hasIdentifier =
                        ont.getObjectProperty(nsName +
                        "#hasIdentifier");
                int i =
                        0;
                while (LOMUtil.getGeneralIdentifier(lom, i) != null) {
                    instance =
                            "#loid-" + inst.getLocalName().substring(3) + "-" +
                            String.valueOf(i);

                    Individual lomIdInstance =
                            ont.createIndividual(nsName +
                            instance, lomIdClass);

                    catalogLOM =
                            LOMUtil.getCatalog(LOMUtil.getGeneralIdentifier(lom,
                            i));
                    entryLOM =
                            LOMUtil.getEntry(LOMUtil.getGeneralIdentifier(lom, i));

                    if (catalogLOM != null) {
                        RDFNode catalogValue =
                                ont.createTypedLiteral(catalogLOM);
                        lomIdInstance.setPropertyValue(catalogProperty,
                                catalogValue);
                    } else {
                        String log =
                                "The Catalog field of the General category must contain a value";
                        errorWriter.println(log);
                    }
                    if (entryLOM != null) {
                        RDFNode entryValue =
                                ont.createTypedLiteral(entryLOM);
                        lomIdInstance.setPropertyValue(entryProperty, entryValue);
                    } else {
                        String log =
                                "The Entry field of the General category must contain a value";
                        errorWriter.println(log);
                    }

                    if ((catalogLOM != null) || (entryLOM != null)) {
                        inst.addProperty(hasIdentifier, lomIdInstance);
                        i++;
                    }
                }

            }

        }

    }
}

